using KL.Core.Common.MyOptions;
using KL.Core.Common.Utils;
using KL.Standard.Domain.MessageQueue;
using KL.Standard.Model.ViewModels.Queue;
using KL.Standard.RabbitMQ;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace KL.Core.Extensions.HostedService
{
     /// <summary>
    /// 消费基本信息队列的任务
    /// </summary>
    public class QueueConcumerService:IHostedService
    {
        //private Timer _scheduler;

        private readonly ILogger<QueueConcumerService> _logger;
        
        private readonly IBaseQueueConcumer<BASEMQ> _baseQueueConcumer;

        private readonly IServiceProvider _provider;

        private AppCustomParaOptions _config;

        public QueueConcumerService( IBaseQueueConcumer<BASEMQ> concumer,IOptions<AppCustomParaOptions> Options,
            ILogger<QueueConcumerService> logger,IServiceProvider provider)
        {
            this._baseQueueConcumer = concumer;
            this._logger = logger;
            this._baseQueueConcumer.StartMQClient();
            this._config = Options.Value;
            this._provider = provider;
            
        }

        public  Task StartAsync(CancellationToken cancellationToken)
        {
            Thread.Sleep(1000);
            _logger.LogInformation("服务{0}启动(消费{1}_Queue队列)...",nameof(QueueConcumerService),_config.SYS_CODE);
            Func<MQueueItem<object>, bool> action =
                (msg =>
                {
                    _logger.LogInformation("消费消息:[synModule]{0}==>[opeType]{1}==>[data]{2}",msg.synModule,msg.opeType,msg.data.ObjToString());
                    // using (var scope = _provider.CreateScope())
                    // {
                        #region 注释
                        // #region 处理account
                        // if (msg.synModule.Equals(SYN_MODULE.ACCOUNT))
                        // {
                        //     var userSvc = scope.ServiceProvider.GetService<IUserSvc>();
                        //     // var msg2 = JsonConvert.DeserializeObject<SysAccountPo>(msg.data.ObjToString());
                        //     // return userSvc.ExecuteSynCommand(msg2, msg.opeType);
                        //     if (msg.opeType.Equals(SYN_OPTTYPE.AUTHOR))
                        //     {
                        //         var msg2 = JsonConvert.DeserializeObject<OutSysAccountAuthorVM>(msg.data.ObjToString());
                        //         return userSvc.UpdataAccountAuthor(msg2);
                        //     }
                        //     else
                        //     {
                        //         var msg2 = JsonConvert.DeserializeObject<SysAccountPo>(msg.data.ObjToString());
                        //         return userSvc.ExecuteSynCommand(msg2, msg.opeType);
                        //     }
                        // }
                        // #endregion
                        // #region 处理dept
                        // if (msg.synModule.Equals(SYN_MODULE.DEPT))
                        // {
                        //     var deptSvc = scope.ServiceProvider.GetService<IDeptSvc>();
                        //     var msg2 = JsonConvert.DeserializeObject<SysDeptPO>(msg.data.ObjToString());
                        //     return deptSvc.ExecuteSynCommand(msg2, msg.opeType);
                        // }
                        // #endregion
                        // #region 处理dept position
                        // if (msg.synModule.Equals(SYN_MODULE.DEPT_POSITION))
                        // {
                        //     var deptSvc = scope.ServiceProvider.GetService<IDeptSvc>();
                        //     var msg2 = JsonConvert.DeserializeObject<SysDeptPositionPO>(msg.data.ObjToString());
                        //     return deptSvc.ExecuteSynCommand4Position(msg2, msg.opeType);
                        // }
                        // #endregion
                        // #region Company
                        // if (msg.synModule.Equals(SYN_MODULE.FTY))
                        // {
                        //     var cmpSvc = scope.ServiceProvider.GetService<ICompanySvc>();
                        //     var msg2 = JsonConvert.DeserializeObject<SysCompanyPO>(msg.data.ObjToString());
                        //     return cmpSvc.ExecuteSynCommand(msg2, msg.opeType);
                        // }
                        // #endregion
                        // #region 处理employee
                        // if (msg.synModule.Equals(SYN_MODULE.EMPLOYEE))
                        // {
                        //     var employeeSvc = scope.ServiceProvider.GetService<IEmployeeSvc>();
                        //     var msg2 = JsonConvert.DeserializeObject<BaseEmployeePO>(msg.data.ObjToString());
                        //     return employeeSvc.ExecuteSynCommand(msg2, msg.opeType);
                        // }
                        // #endregion
                        // #region 处理menu
                        // if (msg.synModule.Equals(SYN_MODULE.MENU))
                        // {
                        //     var svc = scope.ServiceProvider.GetService<IMenuSvc>();
                        //     var jSetting = new JsonSerializerSettings();
                        //     string[] props = { "creatorID", "updatorID", "createdTime", "updatedTime" }; //排除掉，不能让
                        //     jSetting.ContractResolver = new LimitPropsContractResolver(props, false);
                        //     var msg2 = JsonConvert.DeserializeObject<SysMenuPO>(msg.data.ObjToString(), jSetting);
                        //     return svc.ExecuteSynCommand(msg2, msg.opeType);
                        // }
                        // #endregion
                        // #region 处理function
                        // if (msg.synModule.Equals(SYN_MODULE.FUNCTION))
                        // {
                        //     var svc = scope.ServiceProvider.GetService<IMenuFunctionSvc>();
                        //     var jSetting = new JsonSerializerSettings();
                        //     string[] props = { "creatorID", "updatorID", "createdTime", "updatedTime" }; //排除掉，不能让
                        //     jSetting.ContractResolver = new LimitPropsContractResolver(props, false);
                        //     var msg2 = JsonConvert.DeserializeObject<SysMenuFunctionPO>(msg.data.ObjToString(), jSetting);
                        //     return svc.ExecuteSynCommand(msg2, msg.opeType);
                        // }
                        // #endregion
                        // #region 处理role
                        // if (msg.synModule.Equals(SYN_MODULE.ROLE))
                        // {
                        //     var svc = scope.ServiceProvider.GetService<IRoleSvc>();
                        //     var jSetting = new JsonSerializerSettings();
                        //     string[] props = { "creatorID", "updatorID", "createdTime", "updatedTime" }; //排除掉，不能让
                        //     jSetting.ContractResolver = new LimitPropsContractResolver(props, false);
                        //     if (msg.opeType.Equals(SYN_OPTTYPE.AUTHOR))
                        //     {
                        //         var msg2 = JsonConvert.DeserializeObject<SysRoleAuthVM>(msg.data.ObjToString(), jSetting);
                        //         return svc.ExecuteSynRoleAuth(msg2);
                        //     }
                        //     else if (msg.opeType.Equals(SYN_OPTTYPE.COPY))
                        //     {
                        //         var msg2 = JsonConvert.DeserializeObject<SysRoleAuthVM>(msg.data.ObjToString(), jSetting);
                        //         return svc.ExecuteSynCopyRole(msg2);
                        //     }
                        //     else if (msg.opeType.Equals(SYN_OPTTYPE.SAVE) || msg.opeType.Equals(SYN_OPTTYPE.DELETE))
                        //     {
                        //         var msg2 = JsonConvert.DeserializeObject<SysRolePO>(msg.data.ObjToString(), jSetting);
                        //         return svc.ExecuteSynCommand(msg2, msg.opeType);
                        //     }
                        //     return false;
                        // }
                        // #endregion
                        #endregion
                    // }

                    return false;
                });
            _baseQueueConcumer.ConcumeMsg(action, null,$"{_config.SYS_CODE}_QUEUE_NAME");
           

            return Task.CompletedTask;
        }
        
        

        public Task StopAsync(CancellationToken cancellationToken)
        {
            _baseQueueConcumer.CloseClient();
            _logger.LogInformation($"服务{{0}}关闭(消费{{1}}_Queue队列)...时间：{DateTimeOffset.Now}",nameof(QueueConcumerService),_config.SYS_CODE);
            //_logger.LogInformation($"[{DateTimeOffset.Now}]定时任务关闭...");
            return Task.CompletedTask;
        }
    }
}